package xyz.hujf.otherbase.unsafe;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * @author Hujf
 * @title: TestUsafeRelflc
 * @date 2021-04-2315:41
 * @description: TODO
 */
public class TestUsafeRelflc {


    static  Unsafe usa = null;

    static long stateOffset;
    private volatile long state = 0;

    static {
        try {
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            usa = (Unsafe) field.get(null);

            stateOffset = usa.objectFieldOffset(TestUsafeRelflc.class.getDeclaredField("state"));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {

        TestUsafeRelflc a = new TestUsafeRelflc();
        boolean b = usa.compareAndSwapInt(a, stateOffset, 0, 1);
        System.out.println(b);

    }
}
